package com.ganlinux.pangoo.controller.restful.base;

import com.alibaba.fastjson.JSON;
import com.ganlinux.pangoo.dao.model.User;
import com.ganlinux.pangoo.entity.request.ReqHeader;
import com.ganlinux.pangoo.entity.response.base.RspBase;
import com.jfinal.aop.Invocation;
import com.jfinal.ext.interceptor.Restful;

import javax.servlet.http.HttpServletRequest;

/**
 * LoginInterceptor
 * 此拦截器仅做为示例展示，在本 demo 中并不需要
 */
public class BaseInterceptor extends Restful{
	
	public void intercept(Invocation inv) {
		System.out.println("Before invoking " + inv.getActionKey());
		super.intercept(inv);
		System.out.println("After invoking " + inv.getActionKey());
		/*ReqHeader reqHeader = getHeaderFromRequest(inv.getController().getRequest());
		if(reqHeader != null){
			if (User.dao.checkAccess(reqHeader.getPin(),reqHeader.getSign())) {
				super.intercept(inv);
			}else{
				RspBase rspBase = new RspBase();
				rspBase.setStatus(1);
				rspBase.setMessage("身份验证失败");
				inv.getController().renderText(JSON.toJSONString(rspBase));
//				inv.invoke();
			}
		}*/
	}


	public ReqHeader getHeaderFromRequest(HttpServletRequest httpServletRequest){
		ReqHeader reqHeader = new ReqHeader();
		String client = httpServletRequest.getHeader("client");
		reqHeader.setClient(client);
		String interfaceId = httpServletRequest.getHeader("interfaceId");
		reqHeader.setInterfaceId(interfaceId);
		String clientVersion = httpServletRequest.getHeader("clientVersion");
		reqHeader.setClientVersion(clientVersion);
		String buildNum = httpServletRequest.getHeader("buildNum");
		reqHeader.setBuildNum(buildNum);
		String devBrand = httpServletRequest.getHeader("devBrand");
		reqHeader.setDevBrand(devBrand);
		String devModel = httpServletRequest.getHeader("devModel");
		reqHeader.setDevModel(devModel);
		String osVersion = httpServletRequest.getHeader("osVersion");
		reqHeader.setOsVersion(osVersion);
		String screen = httpServletRequest.getHeader("screen");
		reqHeader.setScreen(screen);
		String sign = httpServletRequest.getHeader("sign");
		reqHeader.setSign(sign);
		String netType = httpServletRequest.getHeader("netType");
		reqHeader.setNetType(netType);
		String pin = httpServletRequest.getHeader("pin");
		reqHeader.setPin(pin);

		return reqHeader;
	}
}
